package Chapter7_MultiplexingClass;

/**
 * Date: 2019/2/18
 * Time: 11:46
 * User: Driss.
 *
 * 7.1 组合语法：示例四种初始化对象的方式：在定义对象的地方初始化，在构造器中初始化，惰性初始化（正要使用这些对象之前），实例初始化
 */
class Soap {
    private String s;

    Soap() {
        System.out.println("Soap()");
        s = "Constructed";
    }

    public String toString() {
        return s;
    }
}

public class Bath {

    private String s1 = "Happy",s2 = "Happy" //在定义对象的地方初始化
            ,s3,s4;

    private Soap castille;
    private int i;
    private float toy;

    public Bath(){
        //在构造函数中初始化
        System.out.println("Inside Bath()");
        s3 = "Joy";
        toy = 3.14f;
        castille = new Soap();
    }

    //Instance initialization  实例初始化
    {i = 47;}

    public String toString(){
        if(s4 == null)   //Delayed initialization 延迟初始化
            s4 = "Joy";
        return
                "s1 = " + s1 + "\n" +
                        "s2 = " + s2 + "\n" +
                        "s3 = " + s3 + "\n" +
                        "s4 = " + s4 + "\n" +
                        "i = " + i + "\n" +
                        "toy = " + toy + "\n" +
                        "castille = " + castille;
    }

    public static void main(String[] args) {
        Bath bath = new Bath();
        System.out.println(bath);
    }
}
